package com.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.hospital.dto.LogisticsDto;
import com.hospital.service.intf.LogisticsService;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/logistics/")
public class LogisticsController {

    @Autowired
    private LogisticsService service;
    @PostMapping("add.do")
    public R add(LogisticsDto logisticsDto, HttpServletRequest request){
        return service.add(logisticsDto,request);
    }
    //修改
    @PostMapping("update.do")
    public R update(LogisticsDto logisticsDto){
        return service.update(logisticsDto);
    }//修改

    @PostMapping("updateState.do")
    public R updateState(Integer id ,Integer state){
        return service.updateState(id,state);
    }
    //删除
    @GetMapping("del.do")
    public R del(int id){
        return service.delete(id);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(String info, int page, int limit){
        return service.selectPage(info, page, limit);
    }

    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition","attachment;filename=student-"+System.currentTimeMillis()+".xlsx");
        //2.查询学员数据
        List<LogisticsDto> list=service.all();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(),LogisticsDto.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }

    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }

    @PostMapping("delids.do")
    public R dels(int[] ids){
        return service.delIds(ids);
    }

    @GetMapping("statetj.do")
    public R stateTj(){
        return service.statetj();
    }
}
